//
//  ViewController.swift
//  project2
//
//  Created by 陈强 on 17/5/31.
//  Copyright © 2017年 C_q. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var l_counter: UILabel!
    
    @IBOutlet weak var b_TAP_or_HOLD: UIButton!
    
    var counterNum = 0
    
    var timer = Timer()
    
    
    override func viewDidLoad() {
        super.viewDidLoad()

        let TapGesture = UITapGestureRecognizer(target: self, action: #selector(tap));
        TapGesture.numberOfTapsRequired = 1
        
        let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(long))
        
        b_TAP_or_HOLD.addGestureRecognizer(TapGesture)
        b_TAP_or_HOLD.addGestureRecognizer(longGesture)
        
        updateCounterNum()
        
    }

    
    func tap() {
        
        print("tap")
        
        counterNum = counterNum + 1
        
        updateCounterNum()

    }
    
    func long(sender : UIGestureRecognizer) {
        print("longGesture")
        if sender.state == .ended {
            
            timer.fireDate = NSDate.distantFuture
//            timer.invalidate()
            
        }else if sender.state == .began{
            
            timer.fireDate = NSDate.distantPast
            timer = Timer.scheduledTimer(timeInterval: 0.3, target: self, selector: #selector(longGestureAdd), userInfo: nil, repeats: true)
            
        }
    }
    
    
    func updateCounterNum()  {
        
        l_counter.text = String(counterNum)
        
    }
    
    
    func longGestureAdd()  {
        counterNum += 1
        updateCounterNum()
    }
    
    @IBAction func B_reset() {
        
        counterNum = 0
        updateCounterNum()
        
    }


    
}

